<template>
	<view class="good_list" v-for="item,index in goodList" :key='index' @click="pushRouterDetial(item.goods_id)">
		<view class="good_img">
			<image :src="item.goods_small_logo||mostUrl" mode="" style="width: 100%;height: 100%;"></image>
		</view>
		<view class="good_context">
			<text class="conter_toper">{{item.goods_name}}</text>
			 <text class="conter_num">￥{{item.goods_price}}.00</text>
		</view>
	</view>
</template>

<script>
	import {defineComponent,reactive,markRaw,toRefs,watch} from 'vue'
	import {onReachBottom,onShow,onLoad,onPullDownRefresh} from '@dcloudio/uni-app';

	import easyReauest from '../../Tools/EasyReq.js' 
	export default defineComponent({
		setup(){
			const datas = reactive({
				  queryObj: {
				      query: '',
				      cid: '',
				      pagesize: 10
				    },
					goodList:[],
					pagenum:1,
					total:0,
					requestKey:true,
					mostUrl:'https://s1.chu0.com/src/img/gif/1e/1e65ffa7d15a4d2c94a029269f6aea1e.gif?e=1735488000&token=1srnZGLKZ0Aqlz6dk7yF4SkiYf4eP-YrEOdM1sob:5_nwX-_qLvgE2xcZsLQYkQ-LJ8Y='
				
			})
			function pushRouterDetial(id){
				uni.navigateTo({
					url:`/subpkg/goodsDetial/goodsDetial?id=${id}`
				})
			}
			async function getFloorData(){
				if(datas.goodList.length <=  datas.total && datas.queryObj.pagesize){
					if(datas.requestKey){
						datas.requestKey = false
						if(datas.total && datas.goodList.length && datas.total - datas.goodList.length < 10){
							datas.queryObj.pagesize = datas.total - datas.goodList.length;
							if(!datas.queryObj.pagesize)return 
						}
					try{
						const {query,cid,pagesize} = datas.queryObj
						const rsult = await easyReauest(`/api/public/v1/goods/search?query=${query}&cid=${cid}&pagenum=${datas.pagenum}&pagesize=${pagesize}`)
						datas.goodList = datas.goodList.concat(rsult.goods);
						datas.total = parseInt(rsult.total);
					}catch{
						datas.pagenum -= 1
					}
					datas.requestKey = true
					}
				}
			}
			watch(()=>datas.pagenum,(newValue,oldValue)=>{
				if(typeof datas.pagenum === 'number' && datas.pagenum > 0 && newValue > oldValue){
					getFloorData()
				}
			})
			onPullDownRefresh(async()=>{
				datas.queryObj.pagesize = 10
				datas.goodList = []
				datas.total = 0
				datas.pagenum = 1
				await getFloorData()
				uni.stopPullDownRefresh()
			})
			onLoad(({id='',titel=''})=>{
				datas.queryObj.query = titel
				datas.queryObj.cid = id
				getFloorData()
				uni.setNavigationBarTitle({
					title:'夕夕优购'
				})
			})
			onReachBottom(()=>{
				datas.pagenum += 1
			})
			return {
				...toRefs(datas),pushRouterDetial
			}
			
		}
	})
</script>

<style lang="scss">
	.good_list{
		height: 200rpx;
		width: 100%;
		border-bottom: 1px solid #d8d8df;
		display: flex;
		.good_img{
			flex: 30%;
		}
		.good_context{
			padding: 10rpx 10rpx;
			flex:70%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			.conter_toper{
				font-size: 25rpx;
			}
			.conter_num{
				color: #DD524D;
			}
		}
	}

</style>
